/* Includes ------------------------------------------------------------------*/
#include "log.h"
#include "cx32l003_hal_uart.h"

#include "lpuart.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/

static void SerialSend(uint8_t data);

void logout(bool success)
{
    if(true == success)
    {
        printf("$P\n");
    }
    else
    {
        printf("$F\n");    
    }
}


void SerialSend(uint8_t data) 
{

//    Uart_SendByte(UART0, data);
    
//    Uart_SendByte(UART1, data);
    
     LPUart_Send_Byte(data);
}



#ifdef  __GNUC__
int _write(int file, char *data, int len)
{
     int bytes_written;

     for (bytes_written = 0; bytes_written < len; bytes_written++)
     {
                #ifdef LOG_METHOD_SERIAL
                // expand LF to CR-LF
                if (*data == '\n') {
                        SerialSend('\r');
                }
                SerialSend(*data);
                #else //Another method to log
                        #ifdef LOG_METHOD_RAM
                        *LOG_RAM_CHAR = *data;
                        #endif
                #endif
                data++;
     }

     return bytes_written;
}

#else
int fputc(int ch, FILE *f) {
    
    /* Your implementation of fputc(). */
    #ifdef LOG_METHOD_SERIAL
                    SerialSend(ch);                
    #else //Another method to log
            #ifdef LOG_METHOD_RAM
                    *LOG_RAM_CHAR = *data;
            #endif
    #endif        
    
    return 0;    
}
#endif //__GNUC__


void panic(const char* func)
{
    // If panic info enabled, print it out
    DbgPrintf("Panic call from %s\n",func);
    /* Add any panic string desired or while loop as needed */
}
